package com.wlr.study.leetcode;

public class LeetCode88 {
    public static int[] merge(int[] nums1, int m, int[] nums2, int n) {
        int length = m + n;
        int[] arr = new int[length];
        int  i = 0, j = 0, temp = 0;
        while (i<m||j<n) {
            if (i == m) {
                temp = nums2[j++];
            } else if (j == n) {
                temp = nums1[i++];
            } else if (nums1[i] < nums2[j]) {
                temp = nums1[i++];
            } else {
                temp = nums2[j++];
            }
            arr[i + j - 1] = temp;
        }
        System.arraycopy(arr, 0, nums1, 0, arr.length);
        return nums1;
    }

    public static void main(String[] args) {
//        int[] nums1 = new int[]{1,2,3,0,0,0};
//        int[] nums2 = new int[]{2,5,6};
        int[] nums1 = new int[]{4,5,6,0,0,0};
        int[] nums2 = new int[]{1,2,3};
        int m=3,n=3;
        int[] merge = LeetCode88.merge(nums1, m, nums2, n);
        for (int i : merge) {
            System.out.print(i);
        }
    }
}
